package cn.maihe.elg.operation.utils;

import cn.maihe.elg.operation.model.enums.CenterNoEnum;
import cn.maihe.elg.operation.model.enums.GuaranteeTemplateType;
import cn.maihe.elg.operation.model.enums.RegionInfoEnum;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * @Description 出函工具类
 * @Author SSX
 * @Date 2025/05/30
 */
public class CreateGuaranteeUtil {
    /**
     * 是否国谷
     *
     * @return true 国谷
     */
    public static boolean isGuoGu(CenterNoEnum centerNoEnum) {
        return CenterNoEnum.GUOGU.equals(centerNoEnum);
    }

    /**
     * 是否国谷
     *
     * @return true 国谷
     */
    public static boolean isGuoGuByAcceptOrderNo(String acceptOrderNo) {
        if(StringUtils.isBlank(acceptOrderNo)){
            return false;
        }
        return acceptOrderNo.startsWith(CenterNoEnum.GUOGU.getCenterNo());
    }

    /**
     * 是国谷非随行
     *
     * @return true 是国谷非随行
     */
    public static boolean isGuoGuNonSuiXing(String acceptOrderNo, String clientId) {
        return isGuoGuByAcceptOrderNo(acceptOrderNo) && nonSuiXingByClientId(clientId);
    }

    /**
     * 是国谷非随行
     *
     * @return true 是国谷非随行
     */
    public static boolean isGuoGuNonSuiXing(CenterNoEnum centerNoEnum, String clientId) {
        return isGuoGu(centerNoEnum) && nonSuiXingByClientId(clientId);
    }

    /**
     * 是否随行
     *
     * @return true 随行
     */
    public static boolean isSuiXing(CenterNoEnum centerNoEnum, String clientId) {
        return isGuoGu(centerNoEnum) && isSuiXingByClientId(clientId);
    }

    /**
     * 是否随行
     *
     * @return true 随行
     */
    public static boolean isSuiXingByClientId(String clientId) {
        return RegionInfoEnum.SUI_XING.getRegionId().equals(GuoGuClientInfoUtil.getRegionIdByClientId(clientId));
    }

    /**
     * 不是随行
     *
     * @return true 不是随行
     */
    public static boolean nonSuiXingByClientId(String clientId) {
        return !isSuiXingByClientId(clientId);
    }

    /**
     * 是否随行
     *
     * @return true 随行
     */
    public static boolean isSuiXingByRegionId(String regionId, String comparisonMethod) {
        if(StringUtils.isBlank(regionId)){
            return false;
        }

        if("1".equals(comparisonMethod)){
            if(RegionInfoEnum.SUI_XING.getRegionId().equals(regionId)){
                return true;
            }
        }else if ("2".equals(comparisonMethod)){
            if(regionId.startsWith(RegionInfoEnum.SUI_XING.getRegionId())){
                return true;
            }
        }
        return false;
    }

    /**
     * 是否西安
     *
     * @return true 西安
     */
    public static boolean isXiAn(String acceptOrderNo, String clientId) {
        return isGuoGuByAcceptOrderNo(acceptOrderNo) && RegionInfoEnum.XI_AN.getRegionId().equals(GuoGuClientInfoUtil.getRegionIdByClientId(clientId));
    }

    /**
     * 是否眉山
     * @return true 眉山
     */
    public static boolean isMeiShan(CenterNoEnum centerNoEnum){
        return CenterNoEnum.MEISHAN.equals(centerNoEnum);
    }

    /**
     * 是否眉山
     *
     * @return true 眉山
     */
    public static boolean isMeiShanByAcceptOrderNo(String acceptOrderNo) {
        if(StringUtils.isBlank(acceptOrderNo)){
            return false;
        }
        return acceptOrderNo.startsWith(CenterNoEnum.MEISHAN.getCenterNo());
    }

    /**
     * 金融机构是否放弃申请
     * @return true 放弃
     */
    public static boolean isGiveupNotice(CenterNoEnum centerNoEnum){
        return isMeiShan(centerNoEnum);
    }

    /**
     * 是否（出函过程中）开具发票
     *
     * @return true 开具发票
     */
    public static boolean isIssueInvoice(CenterNoEnum centerNoEnum, String clientId) {
        if(isMeiShan(centerNoEnum)){
            return false;
        }
        if (isGuoGu(centerNoEnum) && StringUtils.isBlank(clientId)) {
            return false;
        }
        return !isGuoGu(centerNoEnum) || isSuiXingByClientId(clientId);
    }

    /**
     * 是否（出函过程中）合成保函OFD（带附件）
     *
     * @return true 合成保函OFD（带附件）
     */
    public static boolean isSynthesisGuaranteeAllOfd(CenterNoEnum centerNoEnum, String clientId) {
        if (isGuoGu(centerNoEnum) && StringUtils.isBlank(clientId)) {
            return false;
        }
        return !isGuoGu(centerNoEnum) || isSuiXingByClientId(clientId);
    }

    /**
     * 是否（出函过程中）设置默认营业执照
     *
     * @return true 设置默认营业执照
     */
    public static boolean isDefaultBusinessLicense(CenterNoEnum centerNoEnum, String clientId) {
        if (isGuoGu(centerNoEnum) && StringUtils.isBlank(clientId)) {
            return true;
        }
        return isGuoGuNonSuiXing(centerNoEnum,clientId);
    }

    /**
     * 保函申请时客户资料是否直接审核通过
     *
     * @return true 审核通过
     */
    public static boolean isAuditPassForCustomerInfo(CenterNoEnum centerNoEnum, String clientId) {
        if (isEPointNingDe(centerNoEnum)) {
            return true;
        } else if (isGuoGu(centerNoEnum) && StringUtils.isBlank(clientId)) {
            return true;
        }
        return isGuoGuNonSuiXing(centerNoEnum,clientId);
    }

    /**
     * 核保通知时是否上传ofd和到账证明
     *
     * @return true 上传ofd和到账证明
     */
    public static boolean isUploadAllOfdAndReceive(CenterNoEnum centerNoEnum, String clientId) {
        return isSuiXing(centerNoEnum,clientId);
    }

    /**
     * 是否设置开标时间
     *
     * @return true 设置开标时间
     */
    public static boolean isSetBidOpenTime(boolean restoreFlag, CenterNoEnum centerNoEnum, String clientId, Integer projectEncryptionFlag){
        if(Objects.isNull(projectEncryptionFlag)){
            projectEncryptionFlag = 0;
        }

        if(isMeiShan(centerNoEnum) && projectEncryptionFlag == 1 && !restoreFlag){
            return true;
        }

        if(isGuoGu(centerNoEnum) && StringUtils.isBlank(clientId) && !restoreFlag){
            return true;
        }

        return isGuoGuNonSuiXing(centerNoEnum,clientId) && !restoreFlag;
    }

    /**
     * 是否宁德新点
     *
     * @return true 宁德新点
     */
    public static boolean isEPointNingDe(CenterNoEnum centerNoEnum) {
        return CenterNoEnum.EPOINT_NINGDE.equals(centerNoEnum);
    }

    /**
     * 项目加密标识
     * @return true 项目加密
     */
    public static boolean getProjectEncryptionFlag(CenterNoEnum centerNoEnum, Integer projectEncryptionFlag){
        if(Objects.isNull(projectEncryptionFlag)){
            projectEncryptionFlag = 0;
        }
        return isMeiShan(centerNoEnum) && projectEncryptionFlag == 0;
    }

    /**
     * 是否厦门
     *
     * @return true 厦门
     */
    public static boolean isXiaMen(CenterNoEnum centerNoEnum) {
        return CenterNoEnum.XIAMEN.equals(centerNoEnum);
    }

    /**
     * 获取国谷模板ID
     *
     * @return 模板ID
     */
    public static Long getGuoGuTemplateId(CenterNoEnum centerNoEnum, String clientId,String secondCategoryCode) {
        Long templateId;
        if (CreateGuaranteeUtil.isSuiXing(centerNoEnum, clientId)) {
            if("01".equals(secondCategoryCode)) {
                templateId = GuaranteeTemplateType.guoGuSuiXingZhuGongChengTemp.getTemplateId();
            }else {
                templateId = GuaranteeTemplateType.guoGuSuiXingTemp.getTemplateId();
            }
        } else {
            templateId = GuaranteeTemplateType.defaultTemp.getTemplateId();
        }
        return templateId;
    }



}
